<?php

namespace App\Admin\Controllers\log;

use App\Admin\Repositories\Log\LogUserCollect;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class LogUserCollectController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new LogUserCollect(), function (Grid $grid) {
            $grid->model()->orderby('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('uid');
            $grid->column('nickname', '会员昵称')->display(function () {
                return isset($this->users->nickname) ? $this->users->nickname : "-";
            });
             $grid->column('result_id')->display(function () {
                 return substr($this->result_id, 1, 100);
             });
            $grid->column('content', '结果内容')->width("50%")->display(function () {
                $this->rid = substr($this->result_id, 1, 100);
                if (substr($this->result_id, 0, 1) == 'i') {
                    $content = "{$this->idata->users->nickname}  {$this->idata->sex} {$this->idata->birthday} {$this->idata->face_score} {$this->idata->constellation} {$this->idata->height} {$this->idata->city} {$this->idata->education} {$this->idata->college} {$this->idata->college_tab} {$this->idata->income} {$this->idata->marriage} {$this->idata->car_and_house} ...";
                } else {
                    $data = json_decode($this->edata->content, true);
                    $sex = $data['sex'] == 1 ? '男' : '女';
                    $nickname = isset($data['nikename']) ? $data['nikename']: "";
                    $content = $nickname . " {$sex} {$data['age']} {$data['constellation']} {$data['height']} {$data['location']['city']} {$data['education']} {$data['school_type']} {$data['annual_income']} {$data['marital_status']} {$data['assets']} ...";
                }
                return $content;
            });
            // $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('uid', '会员id');
//                $filter->like('users.nickname', '会员昵称');
            });
            $grid->export();
            $grid->disableDeleteButton();
            $grid->disableEditButton();
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new LogUserCollect(), function (Show $show) {
            $show->field('id');
            $show->field('uid');
            $show->field('result_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new LogUserCollect(), function (Form $form) {
            $form->display('id');
            $form->text('uid');
            $form->text('result_id');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
